'''
Created on 8 Sep 2009

@author: jack
'''
class PrimitiveBarY:
    def __init__(self, l,w,x0=0,y0=0):
        self.x=x0
        self.y=y0
        self.length=l
        self.width=w
        self.coords=[[self.x,self.y],[self.width+self.x,self.y],[self.width+self.x,self.y+self.length],[self.x,self.y+self.length]]
        self.vertices=len(self.coords)
        self.symmetry=2
        self.struc='bar'
        self.fname=str(self.struc)+'_'+str(l)+'_'+str(w)+'_'   
    def TransShape(self, x,y):
        self.x+=x
        self.y+=y
        for i in range(0, len(self.coords)):
            self.coords[i][0]+=x
            self.coords[i][1]+=y
            
class PrimitiveBtY:
    def __init__(self, l,w,p,x0=0,y0=0):
        pdrop=(w-p)/2
        self.x=x0
        self.y=y0
        self.length=l
        self.width=w
        self.point=p
        self.coords=[[self.x,self.y],[self.width+self.x,self.y],[self.width-pdrop+self.x,+self.length+self.y],[self.x+pdrop,+self.length+self.y]]
        self.vertices=len(self.coords)
        self.symmetry=1 #used to check which flipping method to use.
        self.struc='bt'
        self.fname=str(self.struc)+'_'+str(l)+'_'+str(w)+'_'+str(p)+'_'
    def TransShape(self, x,y):
        self.x+=x
        self.y+=y
        for k in range(0, len(self.coords)):
            self.coords[k][0]+=x
            self.coords[k][1]+=y
    def FlipShapeY(self):
        #x stays the same for bt when flipped in X. Y changes, swaps
        #between the two extremes of the half array, moving inwards, eg 
        #1, n/2-1 swap, 2, n/2-2 swap etc. and then n/2, n swap, n/2+1, n-1 swap
        #so there is a general algorithm here. More symmetry could be exploited for, say,
        #hexagons where to vertices lie on the mirror plane
        #But here we give only a special case of the algorithm, written
        #explicitly.
        self.coords[0][0],self.coords[3][0]=self.coords[3][0],self.coords[0][0]
        self.coords[1][0],self.coords[2][0]=self.coords[2][0],self.coords[1][0]
        

def GenPrim(length=0, width=0,point=0,x=0,y=0,struc='bar'):
    if struc=='bar':
        shape=PrimitiveBarY(length,width,x0=x,y0=y)
    elif struc=='bt':
        shape=PrimitiveBtY(length,width,point,x0=x,y0=y)
    else:
        print 'Error: Specified structure not found. Check for Primitive object'
    return shape

